# 导入蓝图模块
from flask import Blueprint,request,json,jsonify
from db import db1
# 实例化
bp_course = Blueprint("couponM",__name__)

@bp_course.route('/couponM',methods=['POST',"GET","PUT","DELETE"])
def couponM():
    # 添加
    if request.method == "POST":
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form

        sql = "insert into coupon values(0,'%s',%d,'%s','%s')"%(data['name'],int(data['total']),
        data['stime'],data['etime'])

        print(sql)
        db1.insert(sql)
        return 'ok'
    elif request.method == 'PUT':
        # 修改
        # 获取用户输入
        data = request.data
        if data:
            data = json.loads(data)
        else:                                                                                                                                                                                         
            data = request.form
        # 构造sql语句 update 表明 set 字段名1=值，字段名2=值 where id = 1
        sql = "update coupon set name = '%s',total = %d,starttime = '%s',endtime = '%s' where id=%d"%(data['name'],int(data['total']),data['stime'],data['etime'],int(data['id']))
        db1.insert(sql)
        return json.jsonify({"code":200}) 
    elif request.method == "DELETE":
        id = request.args.get('id')
        sql = "delete from coupon where id = %d"%(int(id))
        db1.insert(sql)
        return json.jsonify({"code":200})      



